package view.MainWindow.Conponents;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;

    // 自定义表格工厂类（内部类形式）
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class CustomTableFactory {

    private final String[] columnTitles;
    private final int initialRowCount;
    private final List<Integer> panelColumns = new ArrayList<>();
    private int rowHeight = -1; // 默认不设置行高

    public CustomTableFactory(String[] columnTitles, int initialRowCount) {
        this.columnTitles = columnTitles;
        this.initialRowCount = initialRowCount;
    }

    /**
     * 指定某一列为 Panel 类型（可嵌入 JButton、JComboBox 等组件）
     */
    public CustomTableFactory setPanelAt(int column) {
        if (column >= 0 && column < columnTitles.length) {
            panelColumns.add(column);
        }
        return this;
    }

    /**
     * 设置表格的行高
     */
    public CustomTableFactory setRowHeight(int height) {
        if (height > 0) {
            this.rowHeight = height;
        }
        return this;
    }

    /**
     * 构建并返回封装了表格的 JPanel，可以直接 add 到窗口中
     */
    public JPanel build() {
        // 创建表格模型
        TableModel model = new CustomTableModel(columnTitles, initialRowCount);

        // 创建 JTable
        JTable table = new JTable(model) {
            // 确保组件可以正确绘制
            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component comp = super.prepareRenderer(renderer, row, column);
                if (panelColumns.contains(column)) {
                    if (comp instanceof JComponent) {
                        ((JComponent) comp).setOpaque(false); // 避免背景覆盖
                    }
                }
                return comp;
            }
        };

        // 应用行高
        if (rowHeight > 0) {
            table.setRowHeight(rowHeight);
        }

        // 对 Panel 列应用自定义渲染器和编辑器
        for (int col : panelColumns) {
            table.getColumnModel().getColumn(col).setCellRenderer(new PanelRenderer());
            table.getColumnModel().getColumn(col).setCellEditor(new DefaultCellEditor(new JTextField()) {
                // 使用 Panel 作为编辑组件
                @Override
                public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
                    return value instanceof JPanel ? (JPanel) value : new JPanel();
                }
            });
        }

        // 添加滚动条
        JScrollPane scrollPane = new JScrollPane(table);

        // 返回封装好的 JPanel
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(scrollPane, BorderLayout.CENTER);
        return panel;
    }

    // 内部类：自定义表格模型，支持 Object 类型
    private static class CustomTableModel extends DefaultTableModel {
        public CustomTableModel(String[] columnNames, int rowCount) {
            super(columnNames, rowCount);
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return Object.class; // 支持任何类型的单元格
        }
    }

    // 内部类：Panel 渲染器
    private static class PanelRenderer implements TableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value instanceof JPanel) {
                return (JPanel) value;
            }
            JPanel panel = new JPanel();
            panel.setOpaque(true);
            panel.setBackground(isSelected ? UIManager.getColor("Table.selectionBackground") : UIManager.getColor("Table.background"));
            return panel;
        }
    }
}